﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace SitTight
{
    /// <summary>
    /// Dirt simple program to help with testing the ProcessGong. Will update a file at requested
    /// intervals.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            int timeout = 0;
            FileInfo file = null;

            if (args.Length > 0)
            {
                file = new FileInfo(args[0]);
            }
            if (args.Length > 1)
            {
                timeout = Convert.ToInt32(args[1]);
            }

            ///
            /// Now, create the file if it is there
            /// 

            if (file != null)
            {
                using (var writer = file.CreateText())
                {
                    writer.WriteLine("hi there");
                    writer.Close();
                }
            }

            ///
            /// Now, sleep for however long we need to sleep. We sleep forever if timeout was zero or
            /// not present.
            /// 

            if (timeout == 0)
            {
                while (true)
                {
                    Thread.Sleep(1000);
                }
            }
            else
            {
                while (true)
                {
                    Thread.Sleep(timeout);
                    using (var writer = file.OpenWrite())
                    {
                        StreamWriter wr = new StreamWriter(writer);
                        wr.WriteLine(" update");
                        wr.Close();
                    }
                }
            }
        }
    }
}
